package com.ls.fw.imgserver.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.ls.fw.imgserver.exception.ImageParaException;
import com.ls.fw.web.core.servlet.utils.CoderUtil;
import com.ls.fw.web.core.servlet.utils.ObjectUtil;

public class PathUtil {
	/**保存文件名称*/
	public static final String SAVENAME = "savename";
	
	/**保存文件相对目录*/
	public static final String RELATIVEDIR = "relativeDir";
	
	/**
	 * 获取用户传递的路径参数
	 * @author Defender 
	 * 2014年6月20日下午9:50:51
	 * @param request
	 * @param baseDir
	 * @return
	 */
	public static String getPathPara(HttpServletRequest request) {
		return getPathPara(request,"");
	}
	
	public static String getPathPara(HttpServletRequest request,String para_suffix) {
		String path = getParameter(request, para_suffix+"path");
		String relativePath = getParameter(request, para_suffix+"relativePath");
		if(ObjectUtil.isEmpty(path)){
			path = relativePath;
		}
		return checkPath(path);
	}
	
	
	/**
	 * 获取图片路径
	 * @author Defender 
	 * 2014年6月20日下午9:02:43
	 * @param request
	 * @param baseDir
	 * @return
	 */
	public static String getPath(HttpServletRequest request,String baseDir) {
		return getPath(request,baseDir,"");
	}
	
	public static String getPath(HttpServletRequest request,String baseDir,String para_suffix) {
		String path = getParameter(request, para_suffix+"path");
		String relativePath = getParameter(request, para_suffix+"relativePath");
		String abolutePath = "";
		if(!ObjectUtil.isEmpty(path)){
			try {
				path = CoderUtil.decode(path);
			} catch (Exception e) {
				e.printStackTrace();
			}
			abolutePath = getAbsolutePath(baseDir,path);
		}else if(!ObjectUtil.isEmpty(relativePath)){
			try {
				relativePath = CoderUtil.decode(relativePath);
			} catch (Exception e) {
				e.printStackTrace();
			}
			abolutePath = baseDir+File.separator+relativePath;
		} 
		try {
			abolutePath = CoderUtil.decode(abolutePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return checkPath(abolutePath);
	}
	
	
	/**
	 * 上传文件保存相对目录
	 * @author Defender 
	 * 2014年6月20日下午11:38:18
	 * @param request
	 * @return
	 */
	public String getSaveRelativeDir(HttpServletRequest request) {
		String dir =  getParameter(request, RELATIVEDIR);
		if(!ObjectUtil.isEmpty(dir)){
			if(!dir.startsWith(File.separator)){
				dir = File.separator+dir;
			}
			if(!dir.endsWith(File.separator)){
				dir = dir+File.separator;
			}
		}
		return checkPath(dir);
	}
	
	
	public static boolean getBooleanParameter(HttpServletRequest request,String paramName)throws ImageParaException{
		return getBooleanParameter(request, paramName, false);
	}
	
	public static boolean getBooleanParameter(HttpServletRequest request,String paramName,boolean defaultValue)throws ImageParaException{
		String value = getParameter(request, paramName);
		boolean va = defaultValue;
		if(ObjectUtil.isNotEmpty(value)){
			try {
				va= Boolean.parseBoolean(value);
			} catch (NumberFormatException e) {
				throw new ImageParaException(e);
			}
		}
		return va;
	}
	
	public static int getIntParameter(HttpServletRequest request,String paramName)throws ImageParaException{
		return getIntParameter(request, paramName, 0);
	}
	
	
	public static int getIntParameter(HttpServletRequest request,String paramName,int defaultValue)throws ImageParaException{
		String value = getParameter(request, paramName);
		int va = defaultValue;
		if(ObjectUtil.isNotEmpty(value)){
			try {
				va= Integer.parseInt(value);
			} catch (NumberFormatException e) {
				throw new ImageParaException(e);
			}
		}
		return va;
	}
	
	public static float getFloatParameter(HttpServletRequest request,String paramName)throws ImageParaException{
		return getFloatParameter(request, paramName, 0.0F);
	}
	
	public static float getFloatParameter(HttpServletRequest request,String paramName,float defaultValue)throws ImageParaException{
		String value = getParameter(request, paramName);
		float va = defaultValue;
		if(ObjectUtil.isNotEmpty(value)){
			try {
				va= Float.parseFloat(value);
			} catch (NumberFormatException e) {
				throw new ImageParaException(e);
			}
		}
		return va;
	}
	
	public static double getDoubleParameter(HttpServletRequest request,String paramName,double defaultValue)throws ImageParaException{
		String value = getParameter(request, paramName);
		double va = defaultValue;
		if(ObjectUtil.isNotEmpty(value)){
			try {
				va= Double.parseDouble(value);
			} catch (NumberFormatException e) {
				throw new ImageParaException(e);
			}
		}
		return va;
	}
	
	public static double getDoubleParameter(HttpServletRequest request,String paramName)throws ImageParaException{
		return getDoubleParameter(request, paramName,0.0D);
	}
	
	public static String getParameter(HttpServletRequest request,String paramName){
		String value = request.getParameter(paramName);
		if (value == null) {
			return "";
		}
		try {
			return URLDecoder.decode(value,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return value;
		}
	}
	public static String getAbsolutePath(String baseDir,String path){
		String dir = path;
		int index = 0;
		while(dir.startsWith("/")){
			dir = dir.replaceFirst("/", "");
		}
		while(dir.startsWith("\\")){
			dir = dir.replaceFirst("\\\\", "");
		}
		while(dir.startsWith("../")){
			dir = dir.replaceFirst("../", "");
			index++;
		}
		while(dir.startsWith("..\\")){
			dir = dir.replaceFirst("..\\\\", "");
			index++;
		}
		dir = checkDir(dir);
		String upload = "";
		if(index>0){
			upload = baseDir;
			while(index>0){
				File f = new File(upload);
				upload = f.getParent();
				index--;
			}
			upload = upload+File.separator+dir;
		}else{
			upload = baseDir+File.separator+dir;
		}
		return upload;
	}
	
	public static String checkDir(final String dirPath){
		String dir = dirPath;
	    dir = StringUtils.replaceChars(dir, "\\", File.separator);
	    dir = StringUtils.replaceChars(dir, "/", File.separator);
	    File file = new File(dir);
  		if(!file.exists()){
  			file.mkdirs();
  		}
	    return dir;
	}
	
	public static String checkPath(final String dirPath){
		String dir = dirPath;
		if(ObjectUtil.isEmpty(dir)){
			return "";
		}
	    dir = StringUtils.replaceChars(dir, "\\", File.separator);
	    dir = StringUtils.replaceChars(dir, "/", File.separator);
	    return dir;
	}
}
